<?php
/**
 * Created by PhpStorm.
 * User: merlin
 * Date: 15-11-3
 * Time: 下午3:50
 */
require_once('class.phpmailer.php');
class MailSender
{
    public $mail;
    function initMail($title,$address)
    {
        $this->mail = new PHPMailer(); //实例化
        $this->mail->IsSMTP(); // 启用SMTP
        $this->mail->Host = "smtp.163.com"; //SMTP服务器 以163邮箱为例子
        $this->mail->Port = 25;  //邮件发送端口
        $this->mail->SMTPAuth   = true;  //启用SMTP认证

        $this->mail->CharSet  = "UTF-8"; //字符集
        $this->mail->Encoding = "base64"; //编码方式

       $this->mail->SMTPDebug = 1;
       $this->mail->IsHTML(true);
        $this->mail->Username = "f1120428399";  //你的邮箱
        $this->mail->Password = "fengqiang.123";  //你的密码
        $this->mail->Subject = $title; //邮件标题

        $this->mail->From = "f1120428399@163.com";  //发件人地址（也就是你的邮箱）
       $this->mail->AddAddress($address, "亲");


        $this->mail->FromName = $title;  //发件人姓名

        $this->mail->Body = '你好, <b>朋友</b>! <br/>这是一封来自<a href="http://www.helloweba.com" target="_blank">helloweba.com</a>的邮件！<br/>'; //邮件主体内容

//发送
        if(!$this -> mail->Send()) {
            echo "发送失败: " . $this -> mail->ErrorInfo;
        } else {
            echo "发送成功！";
        }
    }

//监控宝初始化
   /* function initMail($title,$address,$body)
    {
        $this->mail = new PHPMailer(); //实例化
        $this->mail->IsSMTP(); // 启用SMTP
        $this->mail->Host = "smtp.163.com"; //SMTP服务器 以163邮箱为例子
        $this->mail->Port = 25;  //邮件发送端口
        $this->mail->SMTPAuth   = true;  //启用SMTP认证

        $this->mail->CharSet  = "UTF-8"; //字符集
        $this->mail->Encoding = "base64"; //编码方式

        $this->mail->SMTPDebug = 1;
        $this->mail->IsHTML(true);
        $this->mail->Username = "f1120428399";  //你的邮箱
        $this->mail->Password = "fengqiang.123";  //你的密码
        $this->mail->Subject = $title; //邮件标题

        $this->mail->From = "f1120428399@163.com";  //发件人地址（也就是你的邮箱）
        $this->mail->AddAddress($address, "亲");


        $this->mail->FromName = $title;  //发件人姓名

        $this->mail->Body = $body;
        $this -> mail->Send();

    }*/



}//监控宝测试邮件
function test($dirname){
    International_User::instanceLang(2);
    $dir_handle=opendir($dirname);

    $aa = dirname(__FILE__).'/../Smarty.extends.php';

    if(file_exists($aa))
        require_once($aa);
    else
        die("\n no found: $aa");

    $num = 0;
    while($file=readdir($dir_handle))
    {
        $num++;
        if($file!="."&&$file!="..")
        {
            $smarty = new SmartyExtend();
            $aa = new MailSender();
            try{
                $body = $smarty->fetch('mail/custom/'.$file);
                if($body){
                    echo $num."文件指定正确".$file."####\n";
                }
                else{
                    echo $num . '文件指定错误' . $file . "@@@@\n";
                }
            }catch(Exception $e){
                echo $num.'文件指定错误'.$file.$e->getLine()."@@@@\n";
            }
            try{
                $aa->initMail('test'.$num,'1120428399@qq.com',$body);
                echo $num."发送成功".$file."####\n";
            } catch(Exception $e){
                echo '错误信息：'.$e->getMessage()."$$$$$$$$$\n";
                echo $num.'发送失败'.$file."@@@@\n";
            }

        }
    }

    closedir($dir_handle);
}
$a =new MailSender();
$a->initMail('ceshi','1120428399@qq.com');

echo "start&&&&&&&\n";
$a->test('/var/www/jkb_code/www/templates/mail/custom');
die("end@@@@\n");